<!DOCTYPE html>
<html>
<head>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"/>
    <meta charset="UTF-8">
    <title>新建相机检校</title>
    {% include 'template/baseCss.volt' %}
    <link href="/css/vendor/add-drone.less" rel="stylesheet/less" type="text/css">
    <script src="/js/less.min.js"></script>
</head>
<body>
<main>
    <form>
        <div class="title-bar">
            <label class="server-id">新建相机检校</label>
        </div>
        <div id="info-wrap">
            <table>
                <tr>
		            <th>相机id:</th>
		            <td><input type="text" value="" id="camera_id" name="camera_id"/></td>
		        </tr>
		        <tr>
		            <th>镜头id:</th>
		            <td><input type="text" value="" id="camera_lens_id" name="camera_lens_id"/></td>
		        </tr>
		        <tr>
		            <th>相机型号:</th>
		            <td><select name="cameraModel_id" id="cameraModel_id">
		                    <option value="">请选择</option>
		                    {% for id,name in cameraTypeList %}
		                        <option value="{{ id }}">{{ name }}</option>
		                    {% endfor %}
		                </select></td>
		        </tr>
		        <tr>
		            <th>图片1:</th>
		            <td><input type="file" value="" id="file1" name="file1" /></td>
		            <input type="hidden" value="" id="picture1_url" name="picture1_url"/>
		        </tr>
		        <tr>
		            <th>图片2:</th>
		            <td><input type="file" value="" id="file2" name="file2" /></td>
		            <input type="hidden" value="" id="picture2_url" name="picture2_url"/>
		        </tr>
		        <tr>
		            <th>图片3:</th>
		            <td><input type="file" value="" id="file3" name="file3" /></td>
		            <input type="hidden" value="" id="picture3_url" name="picture3_url"/>
		        </tr>
            </table>
            <div class="version-content" id="content"></div>
        </div>
        <p id="save-btn" class="button" style="visibility: visible">增加</p>
        <p id="reset-btn" class="button" style="visibility: visible">重置</p>
    </form>
</main>
</body>

{% include 'template/baseJs.volt' %}
<script type="text/javascript" src="/third-part/wang-editor/wangEditor.min.js"></script>
<script src="/js/cos-js-sdk-v5.js"></script>
<script src="/js/vendor/upload-cos.js"></script>

<script>
    $(function () {
        CosUpload.cosInit("/file/getCosInfo");
        CosUpload.path = CosUpload.path + "cameracheck/";
    });

    $("#reset-btn").click(function (){
        location.reload();
    });

    $("#save-btn").click(function () {
        CosUpload.path = CosUpload.path + "cameracheck/";
        
        var file = $("#file1")[0].files[0];
        if (file) {
            loadingProgressView.show();
            CosUpload.sliceUpload(file, file.name, "cameracheck", function (res) {
                if (res.status === 0) {
                    console.log("上传成功");
                    loadingProgressView.remove();
                    
                    $("#picture1_url").val(res.url);
                    
                    var file = $("#file2")[0].files[0];
                    if (file) {
                        loadingProgressView.show();
                        CosUpload.sliceUpload(file, file.name, "cameracheck", function (res) {
                            if (res.status === 0) {
                                console.log("上传成功");
                                loadingProgressView.remove();
                                
                                $("#picture2_url").val(res.url);
                                
                                var file = $("#file3")[0].files[0];
                                if (file) {
                                    loadingProgressView.show();
                                    CosUpload.sliceUpload(file, file.name, "cameracheck", function (res) {
                                        if (res.status === 0) {
                                            console.log("上传成功");
                                            
                                            var data = {
                                                "action_key": REQUEST_ACTION_ADD,
                                                "camera_id": $("#camera_id").val(),
                                                "camera_lens_id": $("#camera_lens_id").val(),
                                                "cameraModel_id": $("#cameraModel_id").val(),
                                                "picture1_url": $("#picture1_url").val(),
                                                "picture2_url": $("#picture2_url").val(),
                                                "picture3_url": res.url
                                            };
                                            feimaPost("/factory/addCameraCalibrationDo", data, function(result){
                                                if (result.status == 0){
                                                    loadingProgressView.remove();
                                                    window.location.href = "/factory/cameraCalibration";
                                                }else {
                                                    loadingProgressView.remove();
                                                    FAlert(result.message);
                                                }
                                            });
                                        }else {
                                            loadingProgressView.remove();
                                            FAlert("上传文件失败: " + res.error);
                                        }
                                    }, function (progress) {
                                        console.log(progress);
                                        loadingProgressView.setProgress(progress * 0.8 + 0.2);
                                    });
                                }else{
                                	var data = {
                                        "action_key": REQUEST_ACTION_ADD,
                                        "camera_id": $("#camera_id").val(),
                                        "camera_lens_id": $("#camera_lens_id").val(),
                                        "cameraModel_id": $("#cameraModel_id").val(),
                                        "picture1_url": $("#picture1_url").val(),
                                        "picture2_url": $("#picture2_url").val()
                                    };
                                    feimaPost("/factory/addCameraCalibrationDo", data, function(result){
                                        if (result.status == 0){
                                            loadingProgressView.remove();
                                            window.location.href = "/factory/cameraCalibration";
                                        }else {
                                            loadingProgressView.remove();
                                            FAlert(result.message);
                                        }
                                    });
                                }
                            }else {
                                loadingProgressView.remove();
                                FAlert("上传文件失败: " + res.error);
                            }
                        }, function (progress) {
                            console.log(progress);
                            loadingProgressView.setProgress(progress * 0.8 + 0.2);
                        });
                    }else{
                        FAlert("缺少文件2");
                    }
                }else {
                    FAlert("上传文件失败: " + res.error);
                }
            }, function (progress) {
                console.log(progress);
                loadingProgressView.setProgress(progress * 0.8 + 0.2);
            });
        }else{
            FAlert("缺少文件1");
        }
    });
</script>
</html>